package com.dongnao.jack.spring.parse;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ProtocolBeanDefinitionParser implements BaseBeanDefinitionParseInterface {

    private String flag = "protocol";

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {

        if (support(element.getLocalName())) {

            RootBeanDefinition bf = new RootBeanDefinition(ProtocolConfig.class);
            bf.setLazyInit(false);
            String name = element.getAttribute("name");
            String port = element.getAttribute("port");
            String host = element.getAttribute("host");
            bf.getPropertyValues().addPropertyValue("name", name);
            bf.getPropertyValues().addPropertyValue("port", port);
            bf.getPropertyValues().addPropertyValue("host", host);

            parserContext.getRegistry().registerBeanDefinition(name, bf);
            return bf;
        }

        return null;
    }

    @Override
    public boolean support(String elementName) {

        return flag.equals(elementName);
    }

}
